package admin_product;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Add_virtual extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled() throws Exception {
	Actions actions = new Actions(driver);
	driver.manage().window().maximize();
	driver.get(baseUrl + "admin/");
	/*driver.findElement(By.id("username")).clear();
	driver.findElement(By.id("username")).sendKeys("admin");
	driver.findElement(By.id("login")).clear();
	driver.findElement(By.id("login")).sendKeys("admin123");
	driver.findElement(By.cssSelector("input.form-button")).click();
	driver.findElement(By.cssSelector("a[title=\"close\"] > span")).click();*/
	actions.moveToElement(driver.findElement(By.xpath("//ul[@id='nav']/li[3]/a/span"))).build().perform();
	driver.findElement(By.xpath("//ul[@id='nav']/li[3]/ul/li/a/span")).click();
	TimeUnit.SECONDS.sleep(3);
	driver.findElement(By.cssSelector("button[title=\"Add Product\"]")).click();
	TimeUnit.SECONDS.sleep(3);
	new Select(driver.findElement(By.id("product_type"))).selectByVisibleText("Virtual Product");
	driver.findElement(By.cssSelector("button[title=\"Continue\"]")).click();
	TimeUnit.SECONDS.sleep(3);               
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys("Virtual qa test");
    driver.findElement(By.id("sku")).clear();
    driver.findElement(By.id("sku")).sendKeys("virtual_qa_test");
    new Select(driver.findElement(By.id("status"))).selectByVisibleText("Enabled");
    new Select(driver.findElement(By.id("tax_class_id"))).selectByVisibleText("None");
    driver.findElement(By.id("url_key")).clear();
    driver.findElement(By.id("url_key")).sendKeys("virtual_qa_test");
    driver.findElement(By.cssSelector("#product_info_tabs_group_9 > span")).click();
    driver.findElement(By.id("price")).clear();
    driver.findElement(By.id("price")).sendKeys("1500");
    driver.findElement(By.cssSelector("#product_info_tabs_group_95 > span")).click();
    driver.findElement(By.id("description")).clear();
    driver.findElement(By.id("description")).sendKeys("Virtual qa test");
    driver.findElement(By.id("short_description")).clear();
    driver.findElement(By.id("short_description")).sendKeys("Virtual qa test");
    driver.findElement(By.cssSelector("#product_info_tabs_inventory > span")).click();
    driver.findElement(By.id("inventory_qty")).clear();
    driver.findElement(By.id("inventory_qty")).sendKeys("1000");
    new Select(driver.findElement(By.id("inventory_stock_availability"))).selectByVisibleText("In Stock");
    driver.findElement(By.cssSelector("#product_info_tabs_websites > span")).click();
    driver.findElement(By.id("product_website_1")).click();
    driver.findElement(By.cssSelector("#product_info_tabs_customer_options > span")).click();
    driver.findElement(By.cssSelector("button[title=\"Save\"]")).click();
    TimeUnit.SECONDS.sleep(3);
    driver.get(baseUrl + "virtual-qa-test.html");
    assertEquals("Virtual qa test", driver.findElement(By.cssSelector("h1")).getText());
    
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
